package com.example.algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author
 * @DATE 2024/6/30
 **/
public class MatchResult {

    public static void main(String[] args) {
        System.out.println(getMatchResult2("11@2$3@14"));
    }

    public static int getMatchResult(String information) {
        Queue<Integer> queue = new LinkedList<>();
        // 从$切分，注意转义
        String[] info = information.split("\\$");
        for (String s : info) {
            if (s.contains("@")) {
                String[] split = s.split("@");
                int n1 = Integer.parseInt(split[0]);
                int simple = 0;
                for (int i = 1; i < split.length; i++) {
                    simple = 2 * n1 + Integer.parseInt(split[i]) + 3;
                    n1 = simple;
                }
                queue.add(simple);
            } else {
                queue.add(Integer.valueOf(s));
            }
        }

        if (queue.size() == 1) {
            return queue.peek();
        }

        int count = 0;
        // 获取并移除队首元素
        int temp = queue.poll();
        while (!queue.isEmpty()) {
            count = 3 * temp + 2 * queue.poll() + 1;
            temp = count;
        }
        return count;
    }

    public static int getMatchResult2(String information) {
        // 从$切分，注意转义
        String[] exp = information.split("\\$");
        List<Integer> expFinal = new ArrayList<>();
        for (String s : exp) {
            if (s.contains("@")) {
                String[] split = s.split("@");
                expFinal.add(Arrays.stream(split).map(Integer::new).reduce((a, b) -> 2 * a + b + 3).orElse(0));
            } else {
                expFinal.add(Integer.valueOf(s));
            }
        }

        return expFinal.stream().reduce((a, b) -> 3 * a + 2 * b + 1).orElse(0);
    }
}
